import { AnnotationEnum } from './AnnotationEnum';
import 'reflect-metadata';
import { TableStruct, TableStructCache } from '../db/TableStructCache';
import { ColumnType } from '../db/ColumnType';
import { RelTypeEnum } from './RelTypeEnum';
import Exception from '@ohos/zxing/src/main/ets/core/Exception';

const TAG = "CJDBUtil"

export const OneToOne = (v: {
  targetObj: any
  // 关联属性，关联 targetObj 的主键
  joinPropertyName: string,
  joinPropertyTypes: ColumnType,
}): PropertyDecorator => {
  return (target, primaryKey: string) => {

    Reflect.defineMetadata(AnnotationEnum.COLUMN, v.joinPropertyName, target, primaryKey);
    // Reflect.defineMetadata(AnnotationEnum.REL_TYPE, AnnotationEnum.ONE_TO_ONE, target, primaryKey);
    // Reflect.defineMetadata(AnnotationEnum.TYPE, v.types, target, primaryKey);

    // =======================

    // CJLogUtil.info(TAG, "constructor.arguments：" + JSON.stringify(target.constructor.arguments))
    // CJLogUtil.info(TAG, "table cache has：" + TableStructCache.maps.has(target.constructor.name))
    if (!TableStructCache.structMaps.has(target.constructor.name)) {
      TableStructCache.structMaps.set(target.constructor.name, [])
    }
    let caches: Array<TableStruct> = TableStructCache.structMaps.get(target.constructor.name)
    caches.push({
      name: primaryKey,
      columnName: primaryKey + "_id",
      type: v.joinPropertyTypes,
      relProperty: v.joinPropertyName,
      relType: RelTypeEnum.ONE_TO_ONE,
      targetObj: v.targetObj
    })
    // CJLogUtil.info(TAG, "TableStructCache：" + JSON.stringify(TableStructCache.structMaps.get(target.constructor.name)))
  };
}